package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;


import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.adapter.NewsAdapter;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.Contact;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.News;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class KindsActivity extends AppCompatActivity {

    public static final String KINDS_NAME = "kinds_name";
    String responseData;
    RecyclerView recyclerView;
    private List<News> newsList;
    public Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    recyclerView=findViewById(R.id.kinds_news);
                    recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL));
                    NewsAdapter newsAdapter = new NewsAdapter(newsList, getApplicationContext());
                    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                    recyclerView.setAdapter(newsAdapter);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kinds);
        Intent intent = getIntent();
        String kinds_name = intent.getStringExtra(KINDS_NAME);
        Toolbar toolbar = findViewById(R.id.kinds_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(kinds_name);
        if (kinds_name.equals("财经")) {
            okhttp(Contact.KINDS_CAIJING);
        }else if(kinds_name.equals("体育")){
            okhttp(Contact.KINDS_TIYU);
        }else if(kinds_name.equals("娱乐")){
            okhttp(Contact.KINDS_YULE);
        }else if(kinds_name.equals("电影")){
            okhttp(Contact.KINDS_DIANYING);
        }else if(kinds_name.equals("军事")){
            okhttp(Contact.KINDS_JUNSHI);
        }else if(kinds_name.equals("国际")){
            okhttp(Contact.KINDS_GUOJI);
        }else if(kinds_name.equals("科技")){
            okhttp(Contact.KINDS_KEJI);
        }else {
            okhttp(Contact.KINDS_YOUXI);
        }
    }

    private void okhttp(final String kinds) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest(kinds, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        responseData = response.body().string();
                        parseJsonWithGsonObject(responseData);
                    }
                });
            }
        }).start();
    }

    private void parseJsonWithGsonObject(String responseData) {
        {
            Gson gson = new Gson();
            newsList = gson.fromJson(responseData, new TypeToken<List<News>>() {
            }.getType());
            Message msg = new Message();
            msg.what = 1;
            handler.sendMessage(msg);
        }
    }
}
